package com.cls.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cls.business.entity.Commitment;
import com.cls.business.mapper.CommitmentMapper;
import com.cls.business.service.ICommitmentService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class CommitmentServiceImpl extends ServiceImpl<CommitmentMapper, Commitment> implements ICommitmentService {

    private final CommitmentMapper mapper;

    @Override
    public void updateEnable(Commitment commitment) {
//        如果想要发布一个那么其他的都必须改为未发布，只能发布一个
        if (commitment.getIsEnabled()){
            LambdaQueryWrapper<Commitment> updateWrapper = new LambdaQueryWrapper<>();
            updateWrapper.eq(Commitment::getIsEnabled,true);
            Commitment update = new Commitment();
            update.setIsEnabled(false);
            mapper.update(update, updateWrapper);
        }
        mapper.updateById(commitment);

    }
}
